import { IPSControlAction } from '../ipscontrol-action';
import { PSControlContainerImpl } from '../pscontrol-container-impl';
import { IPSLayout } from '../layout/ipslayout';
import { IPSPanelField } from './ipspanel-field';
import { IPSPanelItem } from './ipspanel-item';
import { IPSSysLayoutPanel } from './ipssys-layout-panel';
import { IPSSysPanel } from './ipssys-panel';

export class PSSysPanelImpl extends PSControlContainerImpl implements IPSSysPanel, IPSSysLayoutPanel {
  protected allpspanelfields: IPSPanelField[] | null = null;

  getAllPSPanelFields(): IPSPanelField[] | null {
    if (this.allpspanelfields == null) {
      this.allpspanelfields = this.fillChildListModel(this.M.getAllPSPanelFields, 'control.panel.IPSPanelField') as IPSPanelField[];
    }
    return this.allpspanelfields;
  }

  get allPSPanelFields(): IPSPanelField[] | null {
    return this.getAllPSPanelFields();
  }

  findPSPanelField(objKey: any): IPSPanelField | null {
    return this.getPSModel5('control.panel.IPSPanelField', this.getAllPSPanelFields(), objKey) as IPSPanelField;
  }

  get codeName(): string {
    return this.M.codeName;
  }

  get dataMode(): 0 | 1 | 2 | 3 | 4 | 5 {
    return this.M.dataMode != null ? this.M.dataMode : 0;
  }

  get dataName(): string {
    return this.M.dataName;
  }

  get dataTimer(): number {
    return this.M.dataTimer != null ? this.M.dataTimer : -1;
  }

  protected getpscontrolaction: IPSControlAction | null = null;

  getGetPSControlAction(): IPSControlAction | null {
    if (this.getpscontrolaction != null) return this.getpscontrolaction;
    const value = this.M.getGetPSControlAction;
    if (value == null) {
      return null;
    }
    this.getpscontrolaction = this.getPSControlHandlerMust().findPSControlHandlerAction(value);
    return this.getpscontrolaction;
  }

  get getPSControlAction(): IPSControlAction | null {
    return this.getGetPSControlAction();
  }

  getGetPSControlActionMust(): IPSControlAction {
    const value = this.getGetPSControlAction();
    if (value == null) {
      throw new Error('未指定获取数据行为');
    }
    return value;
  }

  get layoutMode(): 'TABLE' | 'TABLE_12COL' | 'TABLE_24COL' | 'FLEX' | 'BORDER' | 'ABSOLUTE' {
    return this.M.layoutMode;
  }

  protected pslayout: IPSLayout | null = null;

  getPSLayout(): IPSLayout | null {
    if (this.pslayout != null) return this.pslayout;
    const value = this.M.getPSLayout;
    if (value == null) {
      return null;
    }
    this.pslayout = this.getPSModel4('control.layout.IPSLayout', value, 'getPSLayout') as IPSLayout;
    return this.pslayout;
  }

  get psLayout(): IPSLayout | null {
    return this.getPSLayout();
  }

  getPSLayoutMust(): IPSLayout {
    const value = this.getPSLayout();
    if (value == null) {
      throw new Error('未指定面板布局对象');
    }
    return value;
  }

  get panelStyle(): string {
    return this.M.panelStyle;
  }

  get panelWidth(): number {
    return this.M.panelWidth != null ? this.M.panelWidth : 0.0;
  }

  protected rootpspanelitems: IPSPanelItem[] | null = null;

  getRootPSPanelItems(): IPSPanelItem[] | null {
    if (this.rootpspanelitems == null) {
      this.rootpspanelitems = this.fillChildListModel(this.M.getRootPSPanelItems, 'control.panel.IPSPanelItem') as IPSPanelItem[];
    }
    return this.rootpspanelitems;
  }

  get rootPSPanelItems(): IPSPanelItem[] | null {
    return this.getRootPSPanelItems();
  }

  findRootPSPanelItem(objKey: any): IPSPanelItem | null {
    return this.getPSModel5('control.panel.IPSPanelItem', this.getRootPSPanelItems(), objKey) as IPSPanelItem;
  }

  get layoutPanel(): boolean {
    return this.M.layoutPanel != null ? this.M.layoutPanel : false;
  }

  get mobilePanel(): boolean {
    return this.M.mobilePanel != null ? this.M.mobilePanel : false;
  }

  get cls(): string {
    return 'PSSysPanelImpl';
  }

  instanceof(cls: string): boolean {
    if (
      cls == 'control.IPSControl' ||
      cls == 'control.layout.IPSLayoutContainer' ||
      cls == 'control.panel.IPSLayoutPanel' ||
      cls == 'control.panel.IPSPanel' ||
      cls == 'control.panel.IPSSysLayoutPanel' ||
      cls == 'control.panel.IPSSysPanel'
    )
      return true;
    return super.instanceof(cls);
  }
}
